﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MvcPagingExample.Models;

namespace MvcPagingExample.Controllers
{
	public class HomeController : Controller
	{
		[Paging(PageSize = 5)]
		public ActionResult Index(PagingViewModel paging)
		{
			ItemListViewModel<UserViewModel> model = this.GetUsers(paging.PageSize, paging.PageNumber);

			return View(model);
		}

		[ChildActionOnly]
		public ActionResult Paging(PagingViewModel paging)
		{
			return PartialView(paging);
		}

		private List<UserViewModel> users = new List<UserViewModel>()
		{
				new UserViewModel {FirstName="Martin", LastName="Martinů"},
				new UserViewModel {FirstName="Pavel", LastName="Daněk"},	
				new UserViewModel {FirstName="Jan", LastName="Nový"},
				new UserViewModel {FirstName="Jozef", LastName="Marie"},
				new UserViewModel {FirstName="Tom", LastName="Sojer"},
				new UserViewModel {FirstName="Jim", LastName="Gin"},
				new UserViewModel {FirstName="Pepa", LastName="Ydepa"},
				new UserViewModel {FirstName="Jan", LastName="Amos"},
				new UserViewModel {FirstName="Paulus", LastName="Tugent"},
				new UserViewModel {FirstName="Adam", LastName="Evin"},
				new UserViewModel {FirstName="Bořek", LastName="Alík"}
		};

		private ItemListViewModel<UserViewModel> GetUsers(int pageSize, int pageNumber)
		{
			return new ItemListViewModel<UserViewModel>(this.users.Skip(pageSize * (pageNumber - 1)).Take(pageSize), this.users.Count);
		}
	}
}
